package zs.com.wework.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:CookieUtil
 * Package:zs.com.wework
 * Description:此处编写类描述内容
 * Date:2021/11/24 13:34
 * Author:zs
 */
public class CookieUtil{

    WebDriver driver;//尽量不要static的  不然多线程或分布式等测试会有问题

    void genWebDriver(){//根据不同浏览器生成webDriver
        String browserName = System.getenv("browser");

        if(browserName==null||"".equals(browserName)){
            browserName="chrome";
        }

        if("chrome".equals(browserName)){
            System.setProperty("webdriver.chrome.driver","D:/webDriver/bin/chromedriver.exe");
            driver=new ChromeDriver();
        }else if("firefox".equals(browserName)){
            //System.setProperty("webdriver.gecko.driver", "D:/webDriver/bin/geckodriver");
            System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
            driver=new FirefoxDriver();
        }else if("ie".equals(browserName)){
            //System.setProperty("webdriver.ie.driver", "D:/webDriver/bin/IEDriverServer.exe");
            System.setProperty("webdriver.ie.bin", "C:/Program Files/internet explorer");
            driver=new InternetExplorerDriver();
        }

        //    System.out.println("driver:"+driver);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
    }


    @Test
    void genCookies(){//获取cookies 并存到yaml文件；
        try {
            genWebDriver();
            driver.get("https://work.weixin.qq.com/wework_admin/frame");
            Thread.sleep(20000);
            Set<Cookie> cookies = driver.manage().getCookies();
//            cookies.forEach(cookie -> System.out.println(cookie.toJson()+";"));
            cookies.forEach(cookie -> System.out.println(cookie.getName()+" "+cookie.getValue()));
            driver.navigate().refresh();

            ObjectMapper mapper=new ObjectMapper(new YAMLFactory());
            //mapper.writeValue(new File("src/main/resources/cookies.yaml"),cookies);

            String path = this.getClass().getResource("/cookies.yaml").getPath();
           // System.out.println("path:"+path);
            mapper.writeValue(new File(path),cookies);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    void loadCookie(){//从yaml文件中获取cookie 并复用cookie
        try {
            TypeReference<List<HashMap<String, Object>>> listTypeReference = new TypeReference<List<HashMap<String, Object>>>(){

            };
            ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
            //List<HashMap<String, Object>> cookies = mapper.readValue(new File("src/main/resources/cookies.yaml"), listTypeReference);
            List<HashMap<String, Object>> cookies = mapper.readValue(this.getClass().getResource("/cookies.yaml"), listTypeReference);
            genWebDriver();
            driver.get("https://work.weixin.qq.com/wework_admin/frame");
            cookies.forEach(cookie ->
                    driver.manage().addCookie(new Cookie(cookie.get("name").toString(),cookie.get("value").toString()))
                    );
            driver.navigate().refresh();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String path = new CookieUtil().getClass().getResource("/cookies.yaml").getPath();
        System.out.println("path:"+path);
    }

}
